package ar.fiuba.tp7552.buildadmin.webactions.expensas;

import java.util.Collection;

import ar.fiuba.tp7552.buildadmin.model.BusinessException;
import ar.fiuba.tp7552.buildadmin.model.builds.Edificio;
import ar.fiuba.tp7552.buildadmin.model.expensas.TipoExpensa;
import ar.fiuba.tp7552.buildadmin.service.EdificioService;
import ar.fiuba.tp7552.buildadmin.service.ExpensaService;
import ar.fiuba.tp7552.buildadmin.service.PermissionType;
import ar.fiuba.tp7552.buildadmin.webactions.BaseAction;

import com.opensymphony.xwork2.Preparable;

public class LiquidarAction extends BaseAction implements Preparable {

	
	private EdificioService edificioService;
	private ExpensaService expensaService;
	private Edificio edificio = new Edificio();
	private TipoExpensa tipoExpensa;
	private Integer mes;
	private Integer anio;
	// ~~ Constructor ====================================================
	
	public LiquidarAction(EdificioService edificioService, ExpensaService expensaService) {
		super();
		this.edificioService = edificioService;
		this.expensaService = expensaService;
	}
	
	@Override
	public void prepare() throws Exception {
		if (edificio != null && edificio.getId() != null) {
			edificio = edificioService.get(edificio.getId());
		}
	}
	
	public String load() {
		return BaseAction.SUCCESS;
	}
	
	public String liquidar() {
		
		try {
			expensaService.liquidarExpensa(edificio, mes, anio, tipoExpensa);
		} catch (BusinessException e) {
			this.addActionError(e.getMessage());
			return BaseAction.INPUT;
		}
		
		return BaseAction.SUCCESS;
	}
	
	public Collection<Edificio> getEdificios() {
		return edificioService.getAll(PermissionType.COBRO);
	}

	// ~~ getters y setters ========================================================
	public Edificio getEdificio() {
		return edificio;
	}
	public void setEdificio(Edificio edificio) {
		this.edificio = edificio;
	}
	public TipoExpensa getTipoExpensa() {
		return tipoExpensa;
	}
	public void setTipoExpensa(TipoExpensa tipoExpensa) {
		this.tipoExpensa = tipoExpensa;
	}
	public Integer getMes() {
		return mes;
	}
	public void setMes(Integer mes) {
		this.mes = mes;
	}
	public Integer getAnio() {
		return anio;
	}
	public void setAnio(Integer anio) {
		this.anio = anio;
	}
}
